本日重點與方向 (TAG): GCP、Google Cloud Platform、Pub/Sub、Publisher、Subscriber、Tranciver、Reciver、Topic、KEDA、Event Trigger、RabbitMQ、Comsumer
在之前的 GCP 提供的 Pub/Sub 中,我們有提到說事件觸發機制進行拓展的一個機制,後來有看到有人提 KEDA 的這個事件觸發控制器,今年的COSCUP
也有開源貢獻的工程師來演講,有介紹一些事件觸發的概念跟作法,詳細的就去看一下 COSCUP 的議程後續的內容詳述,我們這邊就直接來時實戰一下,這邊預計 KEDA 會弄個兩天做一下測試,我們今天就先去搞一下官方提供的 RabbitMQ 的測試吧,明天會再基於 GCP 的 Pub/Sub 做一個事件觸發的測試。
Helm 安裝
sudo snap install helm
brew install helm
KEDA 安裝
helm repo add kedacore https://kedacore.github.io/charts
root@sdn-k8s-b2-1:~# helm repo add kedacore https://kedacore.github.io/charts
"kedacore" has been added to your repositories
helm repo update
root@sdn-k8s-b2-1:~# helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "kedacore" chart repository
...Successfully got an update from the "istio.io" chart repository
Update Complete. ⎈ Happy Helming!⎈
kubectl create namespace keda
root@sdn-k8s-b2-1:~# kubectl create namespace keda
namespace/keda created
helm install keda kedacore/keda --namespace keda
root@sdn-k8s-b2-1:~# helm install keda kedacore/keda --namespace keda
manifest_sorter.go:192: info: skipping unknown hook: "crd-install"
manifest_sorter.go:192: info: skipping unknown hook: "crd-install"
NAME: keda
LAST DEPLOYED: Thu Jun 4 06:24:14 2020
NAMESPACE: keda
STATUS: deployed
REVISION: 1
TEST SUITE: None
kubectl get deploy -n keda
root@sdn-k8s-b2-1:~# kubectl get deploy -n keda
NAME READY UP-TO-DATE AVAILABLE AGE
keda-operator 1/1 1 1 8m55s
keda-operator-metrics-apiserver 1/1 1 1 8m55s
KEDA 移除
helm uninstall -n keda keda
kubectl delete -f https://raw.githubusercontent.com/kedacore/keda/master/deploy/crds/keda.k8s.io_scaledobjects_crd.yaml
kubectl delete -f https://raw.githubusercontent.com/kedacore/keda/master/deploy/crds/keda.k8s.io_triggerauthentications_crd.yaml
KEDA 安裝
Github Realease Package
kubectl apply -f ./crds
kubectl apply -f ./
Github Repository Clone
git clone https://github.com/kedacore/keda && cd keda
kubectl apply -f deploy/crds/keda.k8s.io_scaledobjects_crd.yaml
kubectl apply -f deploy/crds/keda.k8s.io_triggerauthentications_crd.yaml
kubectl apply -f deploy/
KEDA 移除
kubectl delete -f deploy/crds/keda.k8s.io_scaledobjects_crd.yaml
kubectl delete -f deploy/crds/keda.k8s.io_triggerauthentications_crd.yaml
kubectl delete -f deploy/
helm repo add stable https://kubernetes-charts.storage.googleapis.com
root@sdn-k8s-b2-1:~/sample-go-rabbitmq# helm repo add stable https://kubernetes-charts.storage.googleapis.com
"stable" has been added to your repositories
helm install rabbitmq --set rabbitmq.username=user,rabbitmq.password=PASSWORD stable/rabbitmq
root@sdn-k8s-b2-1:~/sample-go-rabbitmq# helm install rabbitmq --set rabbitmq.username=user,rabbitmq.password=PASSWORD stable/rabbitmq
WARNING: This chart is deprecated
NAME: rabbitmq
LAST DEPLOYED: Thu Jun 4 06:54:28 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
2.1 RabbitMQ bug 修復
部署完之後,會出現 PV 不存在掛不起來的問題,這邊就補一個給他。
生一個 PV 給他
rabbitmq-pv.yaml
----
apiVersion: v1
kind: PersistentVolume
metadata:
name: rabbitmq-pv
spec:
capacity:
storage: 8Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /tmp
部署剛剛配置的 PV
kubectl apply -f rabbitmq-pv.yaml
這邊就是會被拓展的 Deployment 的基底
apiVersion: apps/v1
kind: Deployment
metadata:
name: rabbitmq-consumer
namespace: default
labels:
app: rabbitmq-consumer
spec:
selector:
matchLabels:
app: rabbitmq-consumer
template:
metadata:
labels:
app: rabbitmq-consumer
spec:
containers:
- name: rabbitmq-consumer
image: jeffhollan/rabbitmq-client:dev
imagePullPolicy: Always
command:
- receive
args:
- "amqp://user:PASSWORD@rabbitmq.default.svc.cluster.local:5672"
這邊會對應你的 deployment 名稱,還有一些關於他的拓展行為。
spec.scaleTargetRef.deploymentName
: 對應你的拓展 Deployment 名稱spec.minReplicaCount
: 最小保留 Pod 數spec.maxReplicaCount
: 最大拓展 Pod 數spec.pollingInterval
: 輪詢時間 (每 N 秒向事件的監聽 Server 端確認,駐列中等待消化的訊息數量)spec.cooldownPeriod
: 冷卻時間(過了 N 秒後無事件就開始縮小 Pod 數量)spec.triggers.type
: KEDA 提取的觸發種類spec.triggers.metadata.queueName
: 訂閱的 RabbitMQ 通道名稱spec.triggers.metadata.queueLength
: 每 N 筆訊息開一個 Pod
apiVersion: keda.k8s.io/v1alpha1
kind: ScaledObject
metadata:
name: rabbitmq-consumer
namespace: default
labels:
deploymentName: rabbitmq-consumer
spec:
scaleTargetRef:
deploymentName: rabbitmq-consumer
pollingInterval: 5 # Optional. Default: 30 seconds
cooldownPeriod: 30 # Optional. Default: 300 seconds
maxReplicaCount: 30 # Optional. Default: 100
triggers:
- type: rabbitmq
metadata:
queueName: hello
queueLength: "5"
authenticationRef:
name: rabbitmq-consumer-trigger
這邊就是 KEDA 會去找這邊的資料當作提取你的 RabbitMQ 的使用者權限
apiVersion: keda.k8s.io/v1alpha1
kind: TriggerAuthentication
metadata:
name: rabbitmq-consumer-trigger
namespace: default
spec:
secretTargetRef:
- parameter: host
name: rabbitmq-consumer-secret
key: RabbitMqHost
kubectl create -f deploy/deploy-consumer.yaml
root@sdn-k8s-b2-1:~/sample-go-rabbitmq# kubectl apply -f deploy/deploy-consumer.yaml
secret/rabbitmq-consumer created
deployment.apps/rabbitmq-consumer created
scaledobject.keda.k8s.io/rabbitmq-consumer created
root@sdn-k8s-b2-1:~/sample-go-rabbitmq# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
rabbitmq-consumer 1/1 1 1 5s
kubectl apply -f deploy/deploy-publisher-job.yaml
root@sdn-k8s-b2-1:~/sample-go-rabbitmq# kubectl apply -f deploy/deploy-publisher-job.yaml
job.batch/rabbitmq-publish created
kubectl get deploy -w
root@sdn-k8s-b2-1:~/sample-go-rabbitmq# kubectl get deploy -w
NAME READY UP-TO-DATE AVAILABLE AGE
rabbitmq-consumer 0/1 1 0 16s
rabbitmq-consumer 1/1 1 1 26s
rabbitmq-consumer 0/1 1 0 27s
rabbitmq-consumer 0/0 1 0 29s
rabbitmq-consumer 0/0 1 0 29s
rabbitmq-consumer 0/0 1 0 29s
rabbitmq-consumer 0/0 0 0 29s
rabbitmq-consumer 0/1 0 0 34s
rabbitmq-consumer 0/1 0 0 34s
rabbitmq-consumer 0/1 0 0 34s
rabbitmq-consumer 0/1 1 0 34s
rabbitmq-consumer 1/1 1 1 40s
rabbitmq-consumer 1/4 1 1 45s
rabbitmq-consumer 1/4 1 1 45s
rabbitmq-consumer 1/4 1 1 45s
rabbitmq-consumer 1/4 4 1 45s
rabbitmq-consumer 2/4 4 2 50s
rabbitmq-consumer 3/4 4 3 52s
rabbitmq-consumer 4/4 4 4 55s
rabbitmq-consumer 4/8 4 4 60s
rabbitmq-consumer 4/8 4 4 60s
rabbitmq-consumer 4/8 4 4 60s
rabbitmq-consumer 4/8 8 4 60s
rabbitmq-consumer 5/8 8 5 65s
rabbitmq-consumer 6/8 8 6 68s
rabbitmq-consumer 7/8 8 7 70s
rabbitmq-consumer 8/8 8 8 73s
rabbitmq-consumer 8/16 8 8 75s
rabbitmq-consumer 8/16 8 8 75s
rabbitmq-consumer 8/16 8 8 75s
rabbitmq-consumer 8/16 16 8 75s
rabbitmq-consumer 9/16 16 9 80s
rabbitmq-consumer 10/16 16 10 82s
rabbitmq-consumer 11/16 16 11 84s
rabbitmq-consumer 12/16 16 12 87s
rabbitmq-consumer 13/16 16 13 89s
rabbitmq-consumer 13/30 16 13 90s
rabbitmq-consumer 13/30 16 13 90s
rabbitmq-consumer 13/30 16 13 90s
rabbitmq-consumer 13/30 30 13 90s
rabbitmq-consumer 14/30 30 14 94s
rabbitmq-consumer 15/30 30 15 95s
rabbitmq-consumer 16/30 30 16 97s
rabbitmq-consumer 17/30 30 17 99s
rabbitmq-consumer 18/30 30 18 102s
rabbitmq-consumer 19/30 30 19 104s
rabbitmq-consumer 20/30 30 20 106s
rabbitmq-consumer 21/30 30 21 110s
rabbitmq-consumer 22/30 30 22 112s
rabbitmq-consumer 23/30 30 23 114s
rabbitmq-consumer 24/30 30 24 117s
rabbitmq-consumer 25/30 30 25 119s
rabbitmq-consumer 25/0 30 25 2m
rabbitmq-consumer 25/0 30 25 2m
rabbitmq-consumer 25/0 30 25 2m
rabbitmq-consumer 0/0 0 0 2m